package com.video.netty.InChannelHandler;

import com.alibaba.fastjson.JSONObject;
import com.video.entity.ClientMsg;
import com.video.service.client.IClientService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * 终端上线处理Handler
 *
 * {"id":"online","userid":"1",
 * "terminalname":"1","mac":"00:90:4C:11:22:33",
 * "serverip":"192.168.1.49","ip":"192.168.1.29",
 * "volume":"5","intercomEnable":"1",
 * "cameraIntercomEnable":"1","updateStatus":"1",
 * "dhcpStatus":"1","productsModel":"803"}
 *
 *
 * {"id":"online","userid":"556","terminalname":"556","mac":"0C:2C:54:EF:07:7F",
 * "ip":"192.168.1.100","serverip":"47.107.235.165","volume":"10","intercomEnable":"",
 * "cameraIntercomEnable":"","updateStatus":"V 1.0.46","dhcpStatus":"","productsModel":"1.0.1"}
 */
@BaseChannelHandler.MsgHandler
public class WsOnlineChannelHandler extends BaseChannelHandler {

    @Autowired
    private IClientService clientService;

    @Override
    protected String msgId() {
        return "online";
    }

    @Override
    protected void msgHandler(JSONObject json) {
        //转换成终端对象
        ClientMsg clientMsg = JSONObject.parseObject(json.toJSONString(), ClientMsg.class);
        clientMsg.setId("online");
        clientMsg.setStatus(1);
        if(clientMsg.getTerminalname() != null && clientMsg.getTerminalname().equals("null")){
            clientMsg.setTerminalname(clientMsg.getUserid().toString());
        }
        //保存信息
        clientService.insertOrUpdate(clientMsg);
    }

}
